<?php
class AlbumController extends Pidguin_Controller_Public_Action
{
	public function indexAction()
	{
		if (!$this->_getParam('id')) {
			throw new Zend_Controller_Action_Exception('Album ID is not set');
		}
		
		// find album
		$select = Model_PhotoAlbums::getInstance()->select()->where(
			Model_PhotoAlbums::getDefaultAdapter()->quoteInto('id = ?', $this->_getParam('id'))
		);
		$albumRow = Model_PhotoAlbums::getInstance()->fetchRow($select);
		
		if (!$albumRow) {
			throw new Zend_Controller_Action_Exception('Photo album not found');
		}
		
		$this->view->album = $albumRow;
		
		// set photos
		$photos = array();
		$photos_list = $albumRow->findDependentRowset('Model_AttachmentsForAlbums', 'Album');
		if (count($photos_list) > 0) {
			foreach ($photos_list as $photo) {
				$select = Model_Attachments::getInstance()->select()->from('attachments', array('id', 'url', 'description'));
				$photos[] = $photo->findParentRow('Model_Attachments', 'Attachment', $select);
			}
		}
		
		$this->view->photos = $photos;
	}
}